package com.indexingsystem.boss.utils;


import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.FileChannel;


public class VideoUtils
{

    public static String getFileSize(String remoteFileUrl){
        String fileSize = "";
        try {  
            URL url = new URL(remoteFileUrl);   
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
            conn.setConnectTimeout(10 * 1000);  
            fileSize = conn.getHeaderField("Content-Length");
            System.out.println("文件大小为："+fileSize);  
            } catch (MalformedURLException e) {  
            e.printStackTrace();  
            } catch (IOException e) {  
            e.printStackTrace();  
            }
        return fileSize;
    }
    
//    public static File getFile(String remoteFileUrl){
//        URL url = new URL(remoteFileUrl);
//        // 将url以open方法返回的urlConnection连接强转为HttpURLConnection连接(标识一个url所引用的远程对象连接)
//        // 此时cnnection只是为一个连接对象,待连接中
//        conn = (HttpURLConnection) url.openConnection();
//        // 设置是否要从 URL连接读取数据,默认为true
//        conn.setDoInput(true);
//        // 建立连接
//        // (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
//        conn.connect();
//        // 连接发起请求,处理服务器响应 (从连接获取到输入流)
//        iputstream = conn.getInputStream();
//        // 创建文件输出流，用于保存下载的远程文件
//        oputstream = new FileOutputStream(file);
//    }
    
    // 1、获取视频时长

    public static String ReadVideoTime(String source)
    {
        File file = new File(source);
        Encoder encoder = new Encoder();
        String length = "";
        try
        {
            MultimediaInfo m = encoder.getInfo(file);
            long ls = m.getDuration() / 1000;
            int hour = (int)(ls / 3600);
            int minute = (int)(ls % 3600) / 60;
            int second = (int)(ls - hour * 3600 - minute * 60);
            length = hour + "'" + minute + "''" + second + "'''";
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return length;
    }

    // 2、获取视频大小

    /**
     * 获取视频大小
     * 
     * @param source
     * @return
     */
    public static String ReadVideoSize(String source)
    {
        File file = new File(source);
        FileChannel fc = null;
        String size = "";
        try
        {
            @SuppressWarnings("resource")
            FileInputStream fis = new FileInputStream(file);
            fc = fis.getChannel();
            BigDecimal fileSize = new BigDecimal(fc.size());
            size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != fc)
            {
                try
                {
                    fc.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return size;
    }
}